package com.rambo.baselib.util;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;

import java.util.Optional;

/**
 * Fraction 工具
 */
public class FractionUtils {

    /**
     * 切换Fraction
     *
     * @param fm
     * @param containerResId
     * @param from
     * @param to
     */
    public static void show(FractionManager fm, int containerResId, Fraction from, Fraction to) {
        UiExecutor.getInstance().runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (fm == null) {
                    return;
                }
                if (to == null) {
                    return;
                }
                FractionScheduler fractionScheduler = fm.startFractionScheduler();
                String tag=String.valueOf(to.hashCode());
                Optional<Fraction> fractionOptional = fm.getFractionByTag(tag);
                if (from == null) {
                    if (fractionOptional == null||!fractionOptional.isPresent()) {
                        fractionScheduler.add(containerResId, to, tag).submit();
//                        fractionScheduler.replace(containerResId, to).submit();
                        return;
                    }
                    fractionScheduler.show(to).submit();
                    return;
                }
                fractionScheduler.hide(from);
                LogUtil.d("===hide=== "+from.getClass().getSimpleName());
                if (fractionOptional == null||!fractionOptional.isPresent()) {
                    fractionScheduler.add(containerResId, to, tag).submit();
//                    fractionScheduler.replace(containerResId, to).submit();
                    return;
                }
                fractionScheduler.show(to).submit();
            }
        });

    }
}
